package com.example.gesture

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.gestures.draggable
import androidx.compose.foundation.gestures.rememberDraggableState
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import com.example.gesture.ui.theme.JetpackComposeBasicTheme
import kotlin.math.roundToInt

/**
 * @author huangzehui
 * @date 12/5/2024 下午11:03 周日
 * @description
 *
 * 拖动
 * https://www.bilibili.com/video/BV1ob4y1a7ad?p=65&spm_id_from=pageDriver&vd_source=e07cc8cc94eb22bd76722071b03cd199
 */
class MainActivity03 : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            JetpackComposeBasicTheme {
                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.Center,
                    modifier = Modifier.fillMaxSize()
                ) {
                    DragWhereYouWantSample()
                }
            }
        }
    }


    /**
     * 水平拖拽
     */
    @Composable
    fun DraggableSample() {

        var offsetX by remember {
            mutableFloatStateOf(0f)
        }

        Text(text = "Drag me!", modifier = Modifier
            .offset {
                IntOffset(offsetX.roundToInt(), 0)
            }
            .draggable(
                orientation = Orientation.Horizontal,
                state = rememberDraggableState { delta ->
                    offsetX += delta
                }
            ))
    }

    /**
     * 如果您需:要控制整个拖动手势，请考虑改为通过pointerInput 修饰符使用拖动手势检测器。
     *
     */
    @Composable
    fun DragWhereYouWantSample() {


        Box(modifier = Modifier.fillMaxSize()) {
            var offsetX by remember {
                mutableFloatStateOf(0f)
            }
            var offsetY by remember {
                mutableFloatStateOf(0f)
            }
            Box(modifier = Modifier
                .offset {
                    IntOffset(offsetX.roundToInt(), offsetY.roundToInt())
                }
                .background(Color.Blue)
                .size(50.dp)
                .pointerInput(Unit) {
                    detectDragGestures { change, dragAmount ->
                        offsetX += dragAmount.x
                        offsetY += dragAmount.y
                    }
                }
            ) {

            }
        }
    }

}